package lance.honeydew.action;

import java.util.ArrayList;
import java.util.List;

import lance.honeydew.bean.OrderDetail;
import lance.honeydew.bean.OrderInformation;
import lance.honeydew.bean.ShoppingMoney;
import lance.honeydew.bean.UserMember;
import lance.honeydew.service.ShopService;

import org.apache.log4j.Logger;

public class UserInforAction extends BaseAction {

	private static final Logger logger = Logger.getLogger(UserInforAction.class);
	
	/*Bean*/
	private ShopService shopService;
	private UserMember userMemberBo;
	private List<OrderInformation> orderInforList;
	private List<ShoppingMoney> shoppingMoneyList;
	private Long orderInforSeqid;
	private OrderInformation orderInformationBo;
	private List<OrderDetail> orderDetailList;
	
	
	public List<OrderDetail> getOrderDetailList() {
		return orderDetailList;
	}

	public void setOrderDetailList(List<OrderDetail> orderDetailList) {
		this.orderDetailList = orderDetailList;
	}

	public OrderInformation getOrderInformationBo() {
		return orderInformationBo;
	}

	public void setOrderInformationBo(OrderInformation orderInformationBo) {
		this.orderInformationBo = orderInformationBo;
	}

	public Long getOrderInforSeqid() {
		return orderInforSeqid;
	}

	public void setOrderInforSeqid(Long orderInforSeqid) {
		this.orderInforSeqid = orderInforSeqid;
	}

	public List<OrderInformation> getOrderInforList() {
		return orderInforList;
	}

	public void setOrderInforList(List<OrderInformation> orderInforList) {
		this.orderInforList = orderInforList;
	}

	public List<ShoppingMoney> getShoppingMoneyList() {
		return shoppingMoneyList;
	}

	public void setShoppingMoneyList(List<ShoppingMoney> shoppingMoneyList) {
		this.shoppingMoneyList = shoppingMoneyList;
	}

	public UserMember getUserMemberBo() {
		return userMemberBo;
	}

	public void setUserMemberBo(UserMember userMemberBo) {
		this.userMemberBo = userMemberBo;
	}

	public ShopService getShopService() {
		return shopService;
	}

	public void setShopService(ShopService shopService) {
		this.shopService = shopService;
	}
	
	/*Parameter*/
	private String oldPassword;
	private String newPassword;
	private String comfrimPassword;
	
	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getComfrimPassword() {
		return comfrimPassword;
	}

	public void setComfrimPassword(String comfrimPassword) {
		this.comfrimPassword = comfrimPassword;
	}

	/*Action*/
	public String execute() {
		
		UserMember sessionUser = getSessionUserMember();
		
		if (sessionUser != null) {
			setUserMemberBo(sessionUser);
		} else {
			return ERROR;
		}
		
		return SUCCESS;
	}
	
	/**
	 * userInfor01.jsp
	 * @return
	 */
	public String userInfor01() {
		
		try {
			
			UserMember sessionUser = getSessionUserMember();
			
			if (sessionUser != null) {
				setUserMemberBo(sessionUser);
			} else {
				return ERROR;
			}
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return "userInfor01";
	}
	
	/**
	 * userInfor01.jsp
	 * @return
	 */
	public String userInfor0101() {
		
		try {
			
			if (userMemberBo != null) {
				shopService.updateUserMemberByObj(userMemberBo);
				setSessionUserMember(userMemberBo);
				jsonMessage = "true";
			} else {
				jsonMessage = "false";
			}
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return SUCCESS;
	}
	
	/**
	 * userInfor01.jsp
	 * @return
	 */
	public String userInfor0102() {
		
		try {
			
			if (userMemberBo != null && (oldPassword != null 
					&& newPassword != null 
					&& comfrimPassword != null)) {
				
				UserMember userObj = shopService
						.checkLoginUserMember(userMemberBo.getEmail(), oldPassword);
				
				if (userObj != null) {
					userMemberBo.setPassword(newPassword);
					shopService.updateUserMemberByObj(userMemberBo);
					setSessionUserMember(userMemberBo);
					
					jsonMessage = "true";
				} else {
					jsonMessage = "false";
				}
			} else {
				jsonMessage = "false";
			}
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return SUCCESS;
	}
	
	/**
	 * userInfor02.jsp
	 * @return
	 */
	public String userInfor02() {
		
		try {
			
			UserMember sessionUser = getSessionUserMember();
			
			if (sessionUser != null) {
				
				if (sessionUser.getUserid() != null) {
					
					UserMember userObj = shopService.findUserMemberByUserId(sessionUser.getUserid());
					
					if (userObj.getOrderInformationSet() != null) {
						orderInforList = new ArrayList(userObj.getOrderInformationSet());
					}
					
				} else {
					return ERROR;
				}				
				
			} else {
				return ERROR;
			}
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return "userInfor02";
	}
	
	/**
	 * userInfor02.jsp
	 * @return
	 */
	public String userInfor0201() {
		
		try {
			
			UserMember sessionUser = getSessionUserMember();
			
			if (sessionUser != null) {
				
				if (orderInforSeqid != null) {
					
					OrderInformation orderInforObj = shopService.findOrderInformationBySeqId(orderInforSeqid);
					
					if (orderInforObj != null) {
						
						orderDetailList = new ArrayList(orderInforObj.getOrderDetailSet());
						
					} else {
						return ERROR;
					}
					
				} else {
					return ERROR;
				}
				
			} else {
				return ERROR;
			}
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return "userInfor0201";
	}
	
	/**
	 * userInfor03.jsp
	 * @return
	 */
	public String userInfor03() {
		
		try {
			
			UserMember sessionUser = getSessionUserMember();
			
			if (sessionUser != null) {
				
				UserMember userObj = shopService.findUserMemberByUserId(sessionUser.getUserid());
				
				if (userObj.getShoppingMoneySet() != null) {
					shoppingMoneyList = new ArrayList(userObj.getShoppingMoneySet());
				}
				
			} else {
				return ERROR;
			}
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return "userInfor03";
	}
}
